/**
 * JAVACC DEMO 1.0
 */
package com.apache.passport.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.passport.common.PassPortConst;
import com.apache.passport.entity.LoginInfo;
import com.apache.passport.entity.Token;

/**
 * description:  获取用户上次登录信息(支持外部系统调用,因内部系统直接调会简单点)
 *
 * @author Hou Dayu 创建时间：2016-12-26
 */
public class LastMsgPlugin implements PluginConnector {

    protected IDao loginInfoDao;

    public void setLoginInfoDao(IDao loginInfoDao) {
        this.loginInfoDao = loginInfoDao;
    }

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)
     */
    @Override
    public Object execute(ParamsVo arg0) throws Exception {
        ResultEntity result = new ResultEntity();
        result.setResult("true");
        String toeknId = (String) arg0.getParams("tokenId");
        String userEname = (String) arg0.getParams("userEname");
        if (Validator.isNull(toeknId)) {
            result.setMessage("缺少必要参数:[toeknId]");
            result.setEntity("F");
            return result;
        }
        if (Validator.isNull(toeknId)) {
            result.setMessage("缺少必要参数:[userEname]");
            result.setEntity("F");
            return result;
        }
        Token token = PassPortConst.getToken(toeknId);
        if (Validator.isEmpty(token)) {
            result.setMessage("token已过期或不存在");
            result.setEntity("F");
            return result;
        }

        LoginInfo loginInfos = (LoginInfo) getVoByEname(userEname);

        if (Validator.isEmpty(loginInfos)) {
            result.setMessage("查询用户信息失败,原因或许为用户名不存在");
            result.setEntity("F");
            return result;
        }

        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setLgnJudge(loginInfos.getLgnJudge());
        loginInfo.setLgnLastarea(loginInfos.getLgnLastarea());
        loginInfo.setLgnLastdevice(loginInfos.getLgnLastdevice());
        loginInfo.setLgnLastipaddress(loginInfos.getLgnLastipaddress());
        loginInfo.setLgnLastsys(loginInfos.getLgnLastsys());
        loginInfo.setLgnLasttime(loginInfos.getLgnLasttime());
        loginInfo.setSysEname(loginInfos.getSysEname());
        loginInfo.setUserEname(userEname);

        result.setMessage("查询用户最后登录信息成功");
        result.setEntity(loginInfo);
        return result;
    }

    /**
     * description:  根据用户名去数据库查询用户最后一次的登录信息
     *
     * @param userEname
     * @return
     */
    public Object getVoByEname(String userEname) {
        String sql = loginInfoDao.getSql(3);
        MethodParam param = new MethodParam("ById", "", sql,
                "com.apache.passport.entity.LoginInfo");
        param.setParams("userEname", userEname);
        return loginInfoDao.selectSingle(param);
    }

}
